package com.sikao.app.ui;

import android.os.Bundle;
import android.text.Editable;
import android.text.TextUtils;
import android.text.TextWatcher;
import android.view.Gravity;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.Toast;

import com.cnzhizi.wheelpop.model.OneModel;
import com.cnzhizi.wheelpop.ui.SkmModiPopup;
import com.sikao.app.R;
import com.sikao.app.adapter.MlvMallOrderExtrasItemAdapter;
import com.sikao.app.bean.MallCreateOrderBean;
import com.sikao.app.bean.MallCreateOrderSubmitBean;
import com.sikao.app.bean.MallGoodsAttrChangePriceBean;
import com.sikao.app.bean.MallGoodsAttrChangePriceSubmitBean;
import com.sikao.app.bean.MallGoodsDetailBean;
import com.sikao.app.dao.MallCreatOrderModel;
import com.sikao.app.dao.MallGoodsAttrChangePriceModel;
import com.sikao.app.net.OnNetBackListener;
import com.sikao.app.ui.base.BaseActivity;
import com.sikao.app.utils.JSONUtil;
import com.sikao.app.utils.Utils;
import com.sikao.app.view.MyListView;

import java.util.ArrayList;
import java.util.List;
import java.util.Timer;
import java.util.TimerTask;


/**
 * 商城商品下单  没有加默认版本 没有多选
 */
public class MallOrderActivityBack extends BaseActivity implements View.OnClickListener {

	private TextView titleTv;
	private ImageView backIv;
	private Button orderBtn;
	private MyListView myListView;
	private MallGoodsDetailBean mallGoodsDetailBean;
	private MlvMallOrderExtrasItemAdapter mlvMallOrderExtrasItemAdapter;
	private List<MallGoodsDetailBean.ResBean.GoodsAttrsBean> getGoods_attrs;

	private TextView tv;
	private TextView tvUnit;
	private TextView tv_mall_order_price;
	private TextView tv_mall_order_total_price;
	private EditText et_mall_order_number;

	private MallGoodsAttrChangePriceModel mallGoodsAttrChangePriceModel;
	private MallGoodsAttrChangePriceSubmitBean mallGoodsAttrChangePriceSubmitBean = new MallGoodsAttrChangePriceSubmitBean();
	private List<MallGoodsAttrChangePriceSubmitBean.AttrBean> attr = new ArrayList<MallGoodsAttrChangePriceSubmitBean.AttrBean>();


	private MallCreatOrderModel mallCreatOrderModel;
	private MallCreateOrderSubmitBean mallCreateOrderSubmitBean = new MallCreateOrderSubmitBean();
	private List<MallCreateOrderSubmitBean.AttrBean> attrCreatList = new ArrayList<MallCreateOrderSubmitBean.AttrBean>();

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		setContentView(R.layout.activity_mall_order);
		super.onCreate(savedInstanceState);

	}

	@Override
	protected void initData() {
		mallGoodsDetailBean = (MallGoodsDetailBean) getIntent().getSerializableExtra("mallGoodsDetailBean");

		getGoods_attrs = mallGoodsDetailBean.getRes().getGoods_attrs();


	}

	@Override
	protected void initView() {
		titleTv = (TextView) findViewById(R.id.title_middle_tv);
		backIv = (ImageView) findViewById(R.id.title_left_img);
		orderBtn = (Button) findViewById(R.id.order_btn);
		myListView = (MyListView) findViewById(R.id.mlv_mall_order_extras);
		tv = (TextView) findViewById(R.id.tv);
		tvUnit = (TextView) findViewById(R.id.tv_mall_order_number_unit);

		tv_mall_order_price = (TextView) findViewById(R.id.tv_mall_order_price);
		tv_mall_order_total_price = (TextView) findViewById(R.id.tv_mall_order_total_price);
		et_mall_order_number = (EditText) findViewById(R.id.et_mall_order_number);

	}

	@Override
	protected void initListener() {
		backIv.setOnClickListener(this);
		orderBtn.setOnClickListener(this);
		et_mall_order_number.addTextChangedListener(new TextWatcher() {
			@Override
			public void beforeTextChanged(CharSequence s, int start, int count, int after) {

			}

			@Override
			public void onTextChanged(CharSequence s, int start, int before, int count) {

			}

			@Override
			public void afterTextChanged(Editable s) {
				TimerTask task = new TimerTask() {
					public void run() {
						//execute the task
						attrChangePrice();
					}
				};
				Timer timer = new Timer();
				timer.schedule(task, 1500);
			}
		});
	}


	@Override
	protected void init() {
		titleTv.setText("立即下单");
		backIv.setVisibility(View.VISIBLE);
		tv.setText("总库存"+mallGoodsDetailBean.getRes().getOrigin_number()+mallGoodsDetailBean.getRes().getUnit()+",剩余库存"+mallGoodsDetailBean.getRes().getNumber()+mallGoodsDetailBean.getRes().getUnit());
		tvUnit.setText("采购数量("+mallGoodsDetailBean.getRes().getUnit()+")：");
		et_mall_order_number.setHint("至少采购"+mallGoodsDetailBean.getRes().getLeast_number()+mallGoodsDetailBean.getRes().getUnit());
		Utils.showLog("==============getGoods_attrs" + getGoods_attrs.size());
		//这里只显示 3单选 5下拉 统一处理成 下拉
		if (getGoods_attrs != null && getGoods_attrs.size() > 0) {

			for (int i = 0; i < getGoods_attrs.size(); i++) {
				if (!("3".equals(getGoods_attrs.get(i).getAttr_type())) && !("5".equals(getGoods_attrs.get(i).getAttr_type()))) {
					getGoods_attrs.remove(i);
					i--;
				}

			}
			if (getGoods_attrs.size() > 0) {
				//为content赋值 默认是显示第一个
				for (int i = 0; i < getGoods_attrs.size(); i++) {
					getGoods_attrs.get(i).setContent(getGoods_attrs.get(i).getDefault_value().get(0)+"    浮动值："+getGoods_attrs.get(i).getChange_price().get(0));
					//给提交的参数设置默认值 默认提交第一个
					MallGoodsAttrChangePriceSubmitBean.AttrBean attrBean = new MallGoodsAttrChangePriceSubmitBean.AttrBean();
					attrBean.setId(getGoods_attrs.get(i).getId());
					 attrBean.setValue(getGoods_attrs.get(i).getDefault_value().get(0));

					attr.add(attrBean);
				}
			}
			mlvMallOrderExtrasItemAdapter = new MlvMallOrderExtrasItemAdapter(this, mlvMallOrderExtrasItemAdapterOnClickListener, getGoods_attrs);
			myListView.setAdapter(mlvMallOrderExtrasItemAdapter);
		}
		tv_mall_order_price.setText(mallGoodsDetailBean.getRes().getPrice() + "元");

	}


	int positionItem = 0;//选择的第几条附加值


	final List<OneModel> oneModelList = new ArrayList<OneModel>();//滚轮一级
	public SkmModiPopup skmModiPopup;

	private void showPop() {
		skmModiPopup = new SkmModiPopup(this, skmPopOnClickListener, oneModelList, "选择", 1, null);
		skmModiPopup.showAtLocation(findViewById(R.id.ll_mall_order),
				Gravity.BOTTOM | Gravity.CENTER_HORIZONTAL, 0, 0);
	}


	public SkmModiPopup.SkmPopOnClickListener skmPopOnClickListener = new SkmModiPopup.SkmPopOnClickListener() {

		@Override
		public void myOnClick(int pOneCurrent, int pTwoCurrent, int pThreeCurrent, View v, View root) {
			skmModiPopup.dismiss();
			Utils.showLog("========SkmPopOnClickListener positionItem=" + positionItem + " pOneCurrent=" + pOneCurrent + " oneModelListForGoodsClassify.size" + oneModelList.size());

			//Toast.makeText(MallOrderActivity.this, getGoods_attrs.get(positionItem).getDefault_value().get(pOneCurrent) + "  " + getGoods_attrs.get(positionItem).getId(), Toast.LENGTH_SHORT).show();
			//更新提交参数中修改的这个参数


			MallGoodsAttrChangePriceSubmitBean.AttrBean attrBean = new MallGoodsAttrChangePriceSubmitBean.AttrBean();
			attrBean.setId(getGoods_attrs.get(positionItem).getId());
			attrBean.setValue(getGoods_attrs.get(positionItem).getDefault_value().get(pOneCurrent));
			// attr 和 getGoods_attrs 长度一致
			attr.set(positionItem, attrBean);//替换参数值


			//更改content值 这个字段 如果重新生成bean要重新写上去
			getGoods_attrs.get(positionItem).setContent(getGoods_attrs.get(positionItem).getDefault_value().get(pOneCurrent)+"    浮动值："+getGoods_attrs.get(positionItem).getChange_price().get(pOneCurrent));

			mlvMallOrderExtrasItemAdapter.notifyDataSetChanged();
			//计算价格
			attrChangePrice();
		}
	};
	//adapter的回调
	MlvMallOrderExtrasItemAdapter.MlvMallOrderExtrasItemAdapterOnClickListener mlvMallOrderExtrasItemAdapterOnClickListener = new MlvMallOrderExtrasItemAdapter.MlvMallOrderExtrasItemAdapterOnClickListener() {
		@Override
		public void myOnClick(int position) {
			//Toast.makeText(MallOrderActivity.this, " 点击:" + position, Toast.LENGTH_SHORT).show();
			positionItem = position;
			Utils.showLog("========MlvMallOrderExtrasItemAdapterOnClickListener positionItem=" + positionItem);
			oneModelList.clear();
			for (int i = 0; i < getGoods_attrs.get(positionItem).getDefault_value().size(); i++) {

				OneModel oneModel = new OneModel();
				oneModel.setId(getGoods_attrs.get(positionItem).getId());//属性的id
				oneModel.setName(getGoods_attrs.get(positionItem).getDefault_value().get(i)+"    浮动值："+getGoods_attrs.get(positionItem).getChange_price().get(i));//属性的值
				oneModelList.add(oneModel);
			}
			showPop();

		}
	};


	@Override
	public void onClick(View v) {
		switch (v.getId()) {
			case R.id.title_left_img:
				finish();
				break;
			case R.id.order_btn:
				//调用下单方法
				createOrder();
				break;
			default:
				break;
		}
	}

	/**
	 * 从后台获取计算的价格
	 */
	private void attrChangePrice() {
		mallGoodsAttrChangePriceModel = new MallGoodsAttrChangePriceModel(this, new OnNetBackListener<MallGoodsAttrChangePriceBean>() {
			@Override
			public void netBack(MallGoodsAttrChangePriceBean data) {
				if (data == null) {
					Toast.makeText(MallOrderActivityBack.this, data.getMsg(), Toast.LENGTH_SHORT).show();
				} else {
					if (data.getStatus() == 1) {
						//给单价和总价重新赋值
						tv_mall_order_price.setText(data.getRes().getUnitPrice());
						tv_mall_order_total_price.setText(data.getRes().getTotalPrice());
					} else {
						Toast.makeText(MallOrderActivityBack.this, data.getMsg(), Toast.LENGTH_SHORT).show();
					}
				}
			}
		}
		);
		mallGoodsAttrChangePriceSubmitBean.setType("Coal");
		mallGoodsAttrChangePriceSubmitBean.setGoodsId(mallGoodsDetailBean.getRes().getId());
		mallGoodsAttrChangePriceSubmitBean.setBuyNumber(et_mall_order_number.getText().toString());
		mallGoodsAttrChangePriceSubmitBean.setObjType("goods");

     /*   String value = "";
		for (int i = 0; i < attr.size(); i++) {

			MallGoodsAttrChangePriceSubmitBean.AttrBean attrBean = new MallGoodsAttrChangePriceSubmitBean.AttrBean();
			attrBean.setId(attr.get(i).getId());
			// 取消|
			value = attr.get(i).getValue();
			Utils.showLog("====value前:"+value);
			value = value.substring(0,value.lastIndexOf("    "));
			Utils.showLog("====value后:"+value);
			attrBean.setValue(value);

			attr.set(i,attrBean);

		}*/


		mallGoodsAttrChangePriceSubmitBean.setAttr(attr);
		mallGoodsAttrChangePriceModel.request(JSONUtil.toJSON(mallGoodsAttrChangePriceSubmitBean), false);

	}

	/**
	 * 下单
	 */
	private void createOrder() {
		if (TextUtils.isEmpty(et_mall_order_number.getText())) {
			Toast.makeText(MallOrderActivityBack.this, "数量不能为空", Toast.LENGTH_SHORT).show();
		}
		mallCreatOrderModel = new MallCreatOrderModel(this, new OnNetBackListener<MallCreateOrderBean>() {
			@Override
			public void netBack(MallCreateOrderBean data) {
				if (data == null) {
					Toast.makeText(MallOrderActivityBack.this, data.getMsg(), Toast.LENGTH_SHORT).show();
				} else {
					if (data.getStatus() == 1) {
						Toast.makeText(MallOrderActivityBack.this, data.getMsg(), Toast.LENGTH_SHORT).show();
						//跳转到某个界面
						MallDetailsActivity.mallDetailsActivity.finish();
						finish();
					} else {
						Toast.makeText(MallOrderActivityBack.this, data.getMsg(), Toast.LENGTH_SHORT).show();
					}
				}
			}
		});
		//{"type":"mall","goodsId":"17","price":"370.00","totalPrice":"3700.00","number":"10",
		// "attr":[{"id":"2","value":"7%"},{"id":"3","value":"7000大卡"}]}
		mallCreateOrderSubmitBean.setType("mall");
		mallCreateOrderSubmitBean.setGoodsId(mallGoodsDetailBean.getRes().getId());
		mallCreateOrderSubmitBean.setPrice(tv_mall_order_price.getText().toString());
		mallCreateOrderSubmitBean.setTotalPrice(tv_mall_order_total_price.getText().toString());
		mallCreateOrderSubmitBean.setNumber(et_mall_order_number.getText().toString());

		attrCreatList.clear();

		String value="";
		for (int i = 0; i < attr.size(); i++) {
			MallCreateOrderSubmitBean.AttrBean attrBean = new MallCreateOrderSubmitBean.AttrBean();
			attrBean.setId(attr.get(i).getId());
			attrBean.setValue(attr.get(i).getValue());
			attrCreatList.add(attrBean);
		}
		mallCreateOrderSubmitBean.setAttr(attrCreatList);

		mallCreatOrderModel.request(JSONUtil.toJSON(mallCreateOrderSubmitBean), true);
	}
}

